package com.zb.kc.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.kc.cms.entity.CrmBanner;
import com.zb.kc.cms.service.CrmBannerService;
import com.zb.kc.commonutils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * 后台管理系统
 * </p>
 *
 * @author wanggen
 * @since 2020-04-19
 */
@RestController
@RequestMapping("/educms/banneradmin")
@Slf4j
public class BannerAdminController {


    @Autowired
    private CrmBannerService bannerService;

    /**
     * 分页查询banner
     * @param size
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页查询banner")
    @GetMapping("{size}/{limit}")
    public R pageBanner(@PathVariable("size") long size, @PathVariable long limit) {
        Page<CrmBanner> page = new Page<>(size, limit);
        Page<CrmBanner> bannerPage  = bannerService.page(page);
        return R.ok().dataPage(bannerPage);
    }

    @PostMapping
    @ApiOperation(value = "添加banner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        bannerService.save(crmBanner);
        return R.ok().message("添加banner成功~");
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "根据id获取Banner")
    @GetMapping("{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data(banner);
    }


}
